package com.huanmeiqi.drools.demo.boot;

import com.huanmeiqi.drools.demo.quickstart.Boy;
import com.huanmeiqi.drools.demo.quickstart.Product;
import org.kie.api.runtime.KieSession;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @author cq.Wang
 * @date 2018/7/6 9:32
 * @description
 */
@RestController
public class TestController {

//    @Autowired
//    private KieSession kieSession;

    @Resource
    private ReloadDroolsRules rules;

    @RequestMapping("/testP")
    public void testP() {
        KieSession kieSession = KieUtils.getKieSession();

        Product product = new Product();
        product.setType(Product.DIAMOND);

        kieSession.insert(product);
        int count = kieSession.fireAllRules();
        System.out.println("命中了" + count + "条规则！");
        System.out.println("商品" + product.getType() + "的商品折扣为" + product.getDiscount() + "%。");
    }

    @RequestMapping("/testB/{age}")
    public void testB(@PathVariable("age") Integer age) {
        KieSession kieSession = KieUtils.getKieSession();

        Boy boy = new Boy();
        boy.setAge(age);
        kieSession.insert(boy);
        int count = kieSession.fireAllRules();
        System.out.println("命中了" + count + "条规则！");
        System.out.println("男孩是否允许进入网吧" + boy.getEnterInternetCafe());
    }

    @RequestMapping("/reload/{ruleId}")
    public String reload(@PathVariable("ruleId") Integer ruleId) {
        rules.reload(ruleId);
        return "ok";
    }

    @RequestMapping("/reloadAll")
    public String reloadAll() throws IOException {
        rules.reloadAll();
        return "ok";
    }

    //-------------------以上基本完成根据数据库加载规则----------------------
    // 1、若drl规则中需要操作数据库怎么办？
    // 2、由于insert写入fact是硬编码，不能做到不该源码而适应复杂业务需求的变动
}
